/**
 * 获取节点的子元素节点；
 *
 * @param node dom节点
 * @returns
 */
export function getElementChildren(element: HTMLElement) {
  // no children
  const temp = {
    length: 0,
    push: Array.prototype.push,
    // 兼容IE,对象具有类数组特性
    splice: Array.prototype.splice,
  },
    child = element.childNodes;
  //提高程序运行效率
  for (let i = 0, len = child.length; i < len; i++) {
    //判断节点是否为元素节点
    if (child[i].nodeType === 1) {
      temp.push(child[i]);
    }
  }
  return temp;
}

export function getSiblingElement(element: HTMLElement, n: number) {
  while (element && n) {
    if (n > 0) {
      element = element.nextElementSibling as HTMLElement;
      n--;
    } else {
      element = element.previousElementSibling as HTMLElement;
      n++;
    }
  }
  return element;
}

export function getElementText(element: HTMLElement) {
  return element.textContent;
}

/**
 * 在目标元素下方插入目标元素
 * @param target 目标元素
 * @param element 
 */
export function insertAfter(target: HTMLElement, element: HTMLElement) {
  const parent = element.parentElement;
  const beforeEle = element.nextElementSibling;
  if (beforeEle == null) {
    parent?.appendChild(target);
  } else {
    parent?.insertBefore(target, beforeEle);
  }
}

export function getElementAttribute(node: HTMLElement, name: string) {
  return node.getAttribute(name);
}
